<?php
namespace Home\Controller;
use Think\Controller;
class ZhibiaoweihuController extends Controller {
    
    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {

        $page = I("get.page");
        $limit = I("get.limit");

        $data = M("hospitalsys.guide_name_dict")->limit(($page-1)*$limit,$limit)->select();

        $type_list_2_3 = M("hospitalsys.jxgl_guide_bsc_class_dict")->select();
        
        $type_list_1 = M("hospitalsys.jxgl_guide_organ_class_dict")->select();

        for ($i=0; $i < count($data); $i++) 
        { 
            $info = $data[$i];
            $guide_code = $info['guide_code'];
            $type_id_1 = substr($guide_code,0,1);
            $type_id_2 = substr($guide_code,1,2);
            $type_id_3 = substr($guide_code,1,4);
            for ($j=0; $j < count($type_list_1); $j++) 
            { 
                if($type_list_1[$j]["organ_class_code"] == $type_id_1)
                {
                    $data[$i]['type_1'] = $type_list_1[$j]['organ_class_name'];
                }
            }

            for ($j=0; $j < count($type_list_2_3); $j++) 
            { 
                if($type_list_2_3[$j]["bsc_class_code"] == $type_id_2)
                {
                    $data[$i]['type_2'] = $type_list_2_3[$j]['bsc_class_name'];
                }
            }

            for ($j=0; $j < count($type_list_2_3); $j++) 
            { 
                if($type_list_2_3[$j]["bsc_class_code"] == $type_id_3)
                {
                    $data[$i]['type_3'] = $type_list_2_3[$j]['bsc_class_name'];
                }
            }

        }
        
        

        // for ($i=0; $i < count($data); $i++) 
        // { 
        //     $info = $data[$i];

        //     for ($j=0; $j < count($sys_dept_attr_dict_list); $j++) 
        //     { 

        //         if($info['dept_type'] == $sys_dept_attr_dict_list[$j]['id'])
        //         { 
        //             $data[$i]['dept_type'] = $sys_dept_attr_dict_list[$j]['dept_type'];
        //         }
        //     }

        // }
        
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("hospitalsys.guide_name_dict")->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $guide_code = I("post.guide_code");

        $type_list_2_3 = M("hospitalsys.jxgl_guide_bsc_class_dict")->select();
        
        $type_list_1 = M("hospitalsys.jxgl_guide_organ_class_dict")->select();

        $info = M("hospitalsys.guide_name_dict")->where("guide_code = '$guide_code'")->find();
        
        $guide_code = $info['guide_code'];
        $type_id_1 = substr($guide_code,0,1);
        $type_id_2 = substr($guide_code,1,2);
        $type_id_3 = substr($guide_code,1,4);

        for ($j=0; $j < count($type_list_1); $j++) 
        { 
            if($type_list_1[$j]["organ_class_code"] == $type_id_1)
            {
                $info['type_1'] = $type_list_1[$j]['organ_class_name'];
            }
        }

        for ($j=0; $j < count($type_list_2_3); $j++) 
        { 
            if($type_list_2_3[$j]["bsc_class_code"] == $type_id_2)
            {
                $info['type_2'] = $type_list_2_3[$j]['bsc_class_name'];
            }
        }

        for ($j=0; $j < count($type_list_2_3); $j++) 
        { 
            if($type_list_2_3[$j]["bsc_class_code"] == $type_id_3)
            {
                $info['type_3'] = $type_list_2_3[$j]['bsc_class_name'];
            }
        }

        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");
        unset($data['type_1']);
        unset($data['type_2']);
        unset($data['type_3']);
        $guide_code = $data["guide_code"];
        unset($data["guide_code"]);

        $result = M("hospitalsys.guide_name_dict")->where("guide_code = '$guide_code'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        unset($data['type_1']);
        unset($data['type_2']);
        unset($data['type_3']);
        $data['fixnum'] = 0;
        $result = M("hospitalsys.guide_name_dict")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function set_field()
    {
        $data_all = I("post.");

        $accounting_date    = $data_all["accounting_date"];
        $item_code          = $data_all["item_code"];
        $uuid               = $data_all["uuid"];
        $dept_code          = $data_all["dept_code"];
        $cost_item_code     = $data_all["cost_item_code"];
        $field              = $data_all["field"];


        
        $where['accounting_date'] = strtotime($accounting_date);
        $where['dept_code'] = $dept_code;
        $where['cost_item_code'] = $cost_item_code;

        $info = M("cbhs.cbhs_dept_cost_detail")->where($where)->find();

        if(empty($info))
        {
            $where[$data_all['field']] = $data_all['value'];

            $result = M("cbhs.cbhs_dept_cost_detail")->add($where);
            
            $info = M("cbhs.cbhs_dept_cost_detail")->where($where)->find();

            $d['real_costs'] = $info['costs'] + $info['costs_armyfree'];

            M("cbhs.cbhs_dept_cost_detail")->where($where)->save($d);
        }
        else
        {
            $data[$data_all['field']] = $data_all['value'];

            $result = M("cbhs.cbhs_dept_cost_detail")->where($where)->save($data);
            $info = M("cbhs.cbhs_dept_cost_detail")->where($where)->find();

            $d['real_costs'] = $info['costs'] + $info['costs_armyfree'];
            M("cbhs.cbhs_dept_cost_detail")->where($where)->save($d);
        }
        
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
        $this->ajaxReturn($json,"json");
    }

    public function get_cbhs_cost_item_dict()
    {
        $list = M("cbhs.cbhs_cost_item_dict")->select();
        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }

    public function get_jxgl_guide_bsc_class_dict_list()
    {
        $type = I('post.type');
        $code = I('post.code');

        $list = M("hospitalsys.jxgl_guide_bsc_class_dict")->select();
        $arr = [];
        for ($i=0; $i < count($list); $i++) 
        {
            
            if($type == 1)
            {
                if(strlen($list[$i]['bsc_class_code']) == 2)
                {
                    $arr[] = $list[$i];
                }
            }
            

            if($type == 2)
            {
                if(strlen($list[$i]['bsc_class_code']) == 4)
                {
                    if(!empty($code))
                    {
                        if(substr($list[$i]['bsc_class_code'], 0,2) == $code)
                        {
                            $arr[] = $list[$i];
                        }
                    }
                    else
                    {
                        $arr[] = $list[$i];
                    }
                    
                }
            }
        }

        $json["data"]   = $arr;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }

    public function get_jxgl_guide_organ_class_dict_list()
    {
        $list = M("hospitalsys.jxgl_guide_organ_class_dict")->select();
        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }

    public function get_code()
    {
        $type_1 = I('post.type_1');
        $type_2 = I('post.type_2');
        $type_3 = I('post.type_3');

        $code = $type_1.$type_3;


        $info = M("hospitalsys.guide_name_dict")->where("guide_code like '".$code."%'")->order("guide_code desc")->find();

        if(!empty($info))
        {
            $guide_code = $info['guide_code'];
            $index = substr($guide_code, 5 , 3);

            $index += 1;
            $index = str_pad($index,3,"0",STR_PAD_LEFT);
            $code .= $index;
        }
        else
        {
            $code .= "001";
        }

        $json["data"]   = $code;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");

    }

    public function delete_info()
    {
        $data = I('post.');
        $guide_code = $data['guide_code'];

        $result = M("hospitalsys.guide_name_dict")->where("guide_code = '$guide_code'")->delete();
        if($result !== false)
        {
            $json['code'] = 0;
            $json['msg'] = "删除成功";
        }
        else
        {
            $json['code'] = -1;
            $json['msg'] = "删除失败";
        }
 
        $this->ajaxReturn($json,'json');
    }

    public function get_expressions()
    {
        $guide_code = I("post.guide_code");

        $info = M("hospitalsys.guide_name_dict")->where("guide_code = '$guide_code'")->find();

        $expression_info = M("hospitalsys.guide_expressions")->where("guide_code = '$guide_code'")->find();

        $info['guide_sql'] = $expression_info['guide_sql'];
        $info['guide_sql_sum'] = $expression_info['guide_sql_sum'];

        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_expressions()
    {
        $data = I('post.');
        $guide_code = $data['guide_code'];
        unset($data['guide_name']);
        unset($data['date_text']);
        $info = M("hospitalsys.guide_expressions")->where("guide_code = '$guide_code'")->find();
        if(empty($info))
        {
            $result = M("hospitalsys.guide_expressions")->add($data);
        }
        else
        {
            unset($data['guide_code']);
            $result = M("hospitalsys.guide_expressions")->where("guide_code = '$guide_code'")->save($data);
        }

        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "保存成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "保存失败";
        }
        $this->ajaxReturn($json,"json");

    }

    public function create_data()
    {
        $data = I('post.');
        $data['date'] = date("Ym",strtotime($data['date']));

        
        
        //建立数据库连接
        $user = "system";                //数据库用户名
        $password = "123456";            //密码
        $conn_str = "(DESCRIPTION=(ADDRESS=(PROTOCOL =TCP)(HOST=localhost)(PORT = 1521))(CONNECT_DATA =(SID=orcl)))";          //连接串(cstr : Connection_STRing)
        $remote = true;                  //是否远程连接
        if ($remote) 
        {
          $conn = OCILogon($user, $password, $conn_str);
        }
        else 
        {
          $conn = OCILogon($user, $password);
        }//设定绑定
        $id = 5;                        //准备用以绑定的php变量 id
        $name = "";                     //准备用以绑定的php变量 name/** 调用存储过程的sql语句(sql_sp : SQL_StoreProcedure)
        //  *  语法：
        //  *      BEGIN 存储过程名([[:]参数]); END;
        //  *  加上冒号表示该参数是一个位置
        // **/
        $sql_sp = "BEGIN hospitalsys.guide_value_add(:date, :guide_code); END;";
        //Parse
        $stmt = OCIParse($conn, $sql_sp);//执行绑定
        OCIBindByName($stmt, ":date", $data['date'], 16);           //参数说明：绑定php变量$id到位置:id，并设定绑定长度16位
        OCIBindByName($stmt, ":guide_code", $data['guide_code'], 30);//Execute
        OCIExecute($stmt);

        
        echo 1;
        

    }

    public function get_create_list()
    {
        $page = I("get.page");
        $limit = I("get.limit");
        $date = I('get.date');
        $date = date("Ym",strtotime($date));
        $guide_code = I('get.guide_code');

        
        $data = M("hospitalsys.guide_value")->where("tjyf = '$date' and guide_code = '$guide_code'")->limit(($page-1)*$limit,$limit)->select();

        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("hospitalsys.guide_value")->where("tjyf = '$date' and guide_code = '$guide_code'")->select();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

}